package com.ms3ds.mineInfo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ms3ds.common.core.domain.R;
import com.ms3ds.common.security.utils.SecurityUtils;
import com.ms3ds.mineInfo.api.domain.Mine;
import com.ms3ds.mineInfo.domain.MineUser;
import com.ms3ds.mineInfo.domain.UserMineRel;
import com.ms3ds.system.api.RemoteRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ms3ds.common.log.annotation.Log;
import com.ms3ds.common.log.enums.BusinessType;
import com.ms3ds.common.security.annotation.RequiresPermissions;
import com.ms3ds.mineInfo.service.IMineService;
import com.ms3ds.common.core.web.controller.BaseController;
import com.ms3ds.common.core.web.domain.AjaxResult;
import com.ms3ds.common.core.utils.poi.ExcelUtil;
import com.ms3ds.common.core.web.page.TableDataInfo;

/**
 * 矿山管理Controller
 * 
 * @author zlw
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/mine")
public class MineController extends BaseController
{
    @Autowired
    private IMineService mineService;

    @Autowired
    private RemoteRoleService  remoteRoleService;

    /**
     * 查询矿山管理列表
     */
    @RequiresPermissions("mineInfo:mine:list")
    @GetMapping("/list")
    public TableDataInfo list(Mine mine)
    {
        Long userId = SecurityUtils.getUserId();
        R<Boolean> res = remoteRoleService.isAdmin(userId);
        List<Mine> list = null;
        startPage();
        if(res!=null&&res.getData()){//如果是管理员则查询所有数据
            list = mineService.selectMineList(mine);
        } else {
            list = mineService.selectMineListByUserId(mine,userId);
        }
        return getDataTable(list);
    }

    @GetMapping("/getAll")
    public AjaxResult getAll()
    {
        Long userId = SecurityUtils.getUserId();
        R<Boolean> res = remoteRoleService.isAdmin(userId);
        List<Mine> list = null;
        if(res!=null&&res.getData()){//如果是管理员则查询所有数据
            list = mineService.selectMineList(new Mine());
        } else {
            list = mineService.selectMineListByUserId(new Mine(),userId);
        }
        return success(list);
    }

    /**
     * 获取所有矿山信息，专用于feign接口
     * @return
     */
    @GetMapping("/getAllForRemote")
    public R<List<Mine>> getAllForRemote()
    {
        List<Mine> list = mineService.selectMineList(new Mine());
        return R.ok(list);
    }

    /**
     * 通过mineId获取矿山信息，用于feign接口
     * @param mineId
     * @return
     */
    @GetMapping("/getMineByMineIdForRemote")
    public R<Mine> getMineByMineIdForRemote(Long mineId)
    {
        Mine mine = mineService.selectMineById(mineId);
        return R.ok(mine);
    }

    /**
     * 导出矿山管理列表
     */
    @RequiresPermissions("mineInfo:mine:export")
    @Log(title = "矿山管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Mine mine)
    {
        List<Mine> list = mineService.selectMineList(mine);
        ExcelUtil<Mine> util = new ExcelUtil<Mine>(Mine.class);
        util.exportExcel(response, list, "矿山管理数据");
    }

    /**
     * 获取矿山管理详细信息
     */
    @RequiresPermissions("mineInfo:mine:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mineService.selectMineById(id));
    }

    /**
     * 新增矿山管理
     */
    @RequiresPermissions("mineInfo:mine:add")
    @Log(title = "矿山管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mine mine)
    {
        return toAjax(mineService.insertMine(mine));
    }

    /**
     * 修改矿山管理
     */
    @RequiresPermissions("mineInfo:mine:edit")
    @Log(title = "矿山管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mine mine)
    {
        return toAjax(mineService.updateMine(mine));
    }

    /**
     * 删除矿山管理
     */
    @RequiresPermissions("mineInfo:mine:remove")
    @Log(title = "矿山管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mineService.deleteMineByIds(ids));
    }
    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("mineInfo:mine:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(MineUser user)
    {
        startPage();
        List<MineUser> list = mineService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("mineInfo:mine:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(MineUser user)
    {
        startPage();
        List<MineUser> list = mineService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消授权用户
     */
    @RequiresPermissions("mineInfo:mine:edit")
    @Log(title = "矿山管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody UserMineRel userMine)
    {
        return toAjax(mineService.deleteAuthUser(userMine));
    }

    /**
     * 批量取消授权用户
     */
    @RequiresPermissions("mineInfo:mine:edit")
    @Log(title = "矿山管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(Long mineId, Long[] userIds)
    {
        return toAjax(mineService.deleteAuthUsers(mineId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("mineInfo:mine:edit")
    @Log(title = "矿山管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long mineId, Long[] userIds)
    {
        return toAjax(mineService.insertAuthUsers(mineId, userIds));
    }
}
